package date_202405

import "strings"

//290. 单词规律
//简单
//相关标签
//相关企业
//给定一种规律 pattern 和一个字符串 s ，判断 s 是否遵循相同的规律。
//
//这里的 遵循 指完全匹配，例如， pattern 里的每个字母和字符串 s 中的每个非空单词之间存在着双向连接的对应规律。
//
//
//
//示例1:
//
//输入: pattern = "abba", s = "dog cat cat dog"
//输出: true
//示例 2:
//
//输入:pattern = "abba", s = "dog cat cat fish"
//输出: false
//示例 3:
//
//输入: pattern = "aaaa", s = "dog cat cat dog"
//输出: false
//
//
//提示:
//
//1 <= pattern.length <= 300
//pattern 只包含小写英文字母
//1 <= s.length <= 3000
//s 只包含小写英文字母和 ' '
//s 不包含 任何前导或尾随对空格
//s 中每个单词都被 单个空格 分隔

func wordPattern(pattern string, s string) bool {

	subWords := strings.Split(s, " ")
	if len(subWords) != len(pattern) {
		return false
	}

	runeChars := []rune(pattern)
	wordToCharMapper := make(map[string]int32)
	charToWordMapper := make(map[int32]string)
	for i := 0; i < len(pattern); i++ {
		patternChar := runeChars[i]
		subWord := subWords[i]

		patternMapper, patternMapperExist := charToWordMapper[patternChar]
		wordMapper, wordMapperExist := wordToCharMapper[subWord]
		if patternMapperExist && wordMapperExist &&
			patternMapper == subWord && wordMapper == patternChar {
			continue
		}
		if wordMapperExist || patternMapperExist {
			return false
		}
		wordToCharMapper[subWord] = patternChar
		charToWordMapper[patternChar] = subWord
	}
	return true
}
